package com.zb.demo.service.impl;

import com.zb.dao.FlightMapper;
import com.zb.entity.Flight;
import com.zb.demo.service.FlightService;
import com.zb.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class FlightServiceImpl implements FlightService {
    @Autowired
    private FlightMapper fm;

    @Override
    public PageUtil<Flight> getFlightsByConds(Integer pageIndex,Integer pageSize,Integer departureCity,Integer arrivalCity) {
        PageUtil<Flight> pageUtil=new PageUtil<Flight>();
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("startNo",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("departureCity",departureCity);
        map.put("arrivalCity",arrivalCity);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setPageSize(pageSize);
        pageUtil.setTotalCount(fm.getFlightCount(map));
        pageUtil.setDatas(fm.getFlightsByConds(map));
        return pageUtil;
    }

    @Override
    public Map<String, Object> add(Flight flight) {
        Map<String, Object> map=new HashMap<String, Object>();
        int count=fm.add(flight);
        if(count>0){
            map.put("status",1);
            map.put("info","添加成功");
        }else{
            map.put("status",0);
            map.put("info","添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> getFlightByflightNo(String flightNo) {
        Map<String, Object> map=new HashMap<String, Object>();
        Flight flight = fm.getFlightByflightNo(flightNo);
        if(flight!=null){
            map.put("status",1);
            map.put("info","航班编号已存在");
        }else{
            map.put("status",0);
            map.put("info","");
        }
        return map;
    }
}
